﻿using System;
using System.Reflection;
using System.Text;

namespace NativeCode.Core.Extensions
{
	public static class DebuggingExtensions
	{
		/// <summary>
		/// Spits out a two-column table of property/value pairs.
		/// </summary>
		/// <param name="instance"></param>
		/// <returns></returns>
		public static string ToHtmlTable(this object instance)
		{
			StringBuilder table = new StringBuilder();
			Type type = instance.GetType();

			table.AppendLine("<table class=\"debug\"><thead><th><td>Property</td><td>Value</td></th></thead><tbody>");

			PropertyInfo[] properties = type.GetProperties();
			foreach (PropertyInfo property in properties)
			{
				table.AppendFormat("<tr><td>{0}</td><td>{1}</td></tr>", property.Name, property.GetValue(instance, new object[] {}));
			}

			table.AppendLine("</tbody></table>");
			return table.ToString();
		}

		/// <summary>
		/// Emits one TR set of tags as output based on the object's properties.
		/// </summary>
		/// <param name="instance"></param>
		/// <returns></returns>
		public static string ToHtmlRow(this object instance)
		{
			StringBuilder table = new StringBuilder();
			Type type = instance.GetType();

			table.Append("<tr class=\"debug\">");
			PropertyInfo[] properties = type.GetProperties();
			foreach (PropertyInfo property in properties)
			{
				table.AppendFormat("<td>{0}</td>", property.GetValue(instance, new object[] {}));
			}
			table.Append("</tr>");

			return table.ToString();
		}
	}
}